package com.ieasy.module.oa.project.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.ieasy.basic.dao.IBaseDao;
import com.ieasy.basic.model.DataGrid;
import com.ieasy.basic.model.Msg;
import com.ieasy.basic.util.NumberUtil;
import com.ieasy.basic.util.UUIDHexGenerator;
import com.ieasy.module.common.service.BaseService;
import com.ieasy.module.oa.project.entity.PersonBudget;
import com.ieasy.module.oa.project.web.form.ComapnyMemberCountForm;
import com.ieasy.module.oa.project.web.form.PersonBudgetForm;
import com.ieasy.module.system.entity.PersonEntity;
import com.ieasy.module.system.web.form.PersonForm;

@Service
public class PersonBudgetService extends BaseService implements IPersonBudgetService {
	
	@Inject
	private IBaseDao<PersonBudget> basedaoPersonBudget;
	
	@Inject
	private IBaseDao<PersonEntity> basedaoPerson ;
	
	@Inject
	private IComapnyMemberCountService comapnyMemberCountService ;

	@Override
	public Msg addOrUpdate(PersonBudgetForm form) {
		Msg msg = new Msg() ;
		
		//根据工号获得人员对于的ID
		PersonForm p = (PersonForm) this.basedaoPerson.queryObjectSQL("select t.id, t.num, t.name from ieasy_sys_person t where t.num=?", new Object[]{form.getPersonId()}, PersonForm.class, false) ;
		if(null == p) {
			msg.setMsg("工号不能为空");
			msg.setStatus(false);
			return msg;
		}
		//将工号设置为人员ID
		form.setPersonId(p.getId());
		
		//检查该用户是否存在（年份，人员ID）
		List<PersonBudget> checkPerson = this.basedaoPersonBudget.list("select t from PersonBudget t where t.year=? and t.personId=?", new Object[]{form.getYear(), form.getPersonId()}) ;
		
		//存在这修改数据
		if(null != checkPerson && checkPerson.size() > 0) {
			String sql1 = "update ieasy_oa_person_budget set "+form.getMonth()+"=? where year=? and personId=? and category='deptCount'" ;
			this.basedaoPersonBudget.executeSQL(sql1, new Object[]{form.getDeptCount(), form.getYear(), form.getPersonId()}) ;
			
			String sql2 = "update ieasy_oa_person_budget set "+form.getMonth()+"=? where year=? and personId=? and category='maxMember'" ;
			this.basedaoPersonBudget.executeSQL(sql2, new Object[]{form.getMaxMember(), form.getYear(), form.getPersonId()}) ;
			
			String sql3 = "update ieasy_oa_person_budget set "+form.getMonth()+"=? where year=? and personId=? and category='minMember'" ;
			this.basedaoPersonBudget.executeSQL(sql3, new Object[]{form.getMinMember(), form.getYear(), form.getPersonId()}) ;
		} else {//不存在这创建
			String sql1 = "insert into ieasy_oa_person_budget(id, personId, year, "+form.getMonth()+", category) values(?,?,?,?,'deptCount')" ;
			this.basedaoPersonBudget.executeSQL(sql1, new Object[]{UUIDHexGenerator.generator(), form.getPersonId(), form.getYear(), form.getDeptCount()}) ;
			
			String sql2 = "insert into ieasy_oa_person_budget(id, personId, year, "+form.getMonth()+", category) values(?,?,?,?,'maxMember')" ;
			this.basedaoPersonBudget.executeSQL(sql2, new Object[]{UUIDHexGenerator.generator(), form.getPersonId(), form.getYear(), form.getMaxMember()}) ;
			
			String sql3 = "insert into ieasy_oa_person_budget(id, personId, year, "+form.getMonth()+", category) values(?,?,?,?,'minMember')" ;
			this.basedaoPersonBudget.executeSQL(sql3, new Object[]{UUIDHexGenerator.generator(), form.getPersonId(), form.getYear(), form.getMinMember()}) ;
		}
		
		msg.setMsg("提交成功");
		msg.setStatus(true);
		return msg;
	}
	
	@Override
	public List<PersonBudgetForm> getAllPersonBudgetList(Integer searchYear) {
		List<PersonBudgetForm> deptList = new ArrayList<PersonBudgetForm>() ;
		
		String personSql = "SELECT "
				+ "b.personId, p.name as personName, o.name as deptName "
				+ "FROM ieasy_oa_person_budget b "
				+ "LEFT JOIN ieasy_sys_person p on(p.id=b.personId) "
				+ "LEFT JOIN ieasy_sys_org o on(o.id=p.org_id) "
				+ "WHERE b.year=? "
				+ "GROUP BY b.personId "
				+ "ORDER BY o.name" ;
		List<PersonBudgetForm> deptPersonList = this.basedaoPersonBudget.listSQL(personSql, new Object[]{searchYear}, PersonBudgetForm.class, false);
		for (PersonBudgetForm p : deptPersonList) {
			
			PersonBudgetForm form = new PersonBudgetForm() ;
			form.setPersonId(p.getPersonId());
			form.setSearchYear(searchYear);
			
			List<Map<String, Object>> personBudget = this.getPersonBudget(form) ;
			p.setChildren(personBudget);
			
			deptList.add(p) ;
		}
		return deptList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getAllPersonBudgetReport(Integer searchYear) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>() ;
		
		Map<String, Object> m1 = new HashMap<String, Object>() ;
		m1.put("category", "部門预订人数（人）") ;
		Map<String, Object> m2 = new HashMap<String, Object>() ;
		m2.put("category", "最大需求人数（人）") ;
		Map<String, Object> m3 = new HashMap<String, Object>() ;
		m3.put("category", "最小需求人数（人）") ;
		
		Map<String, Object> m4 = new HashMap<String, Object>() ;
		m4.put("category", "需求平均（人）") ;
		Map<String, Object> m5 = new HashMap<String, Object>() ;
		m5.put("category", "最大空闲人数（人）") ;
		Map<String, Object> m6 = new HashMap<String, Object>() ;
		m6.put("category", "最小空闲人数（人）") ;
		Map<String, Object> m7 = new HashMap<String, Object>() ;
		m7.put("category", "空闲平均（人）") ;
		
		Map<String, Object> m11 = new HashMap<String, Object>() ;
		m11.put("category", "WZN全社合計人数（人）") ;
		
		//各地区在职人数
		ComapnyMemberCountForm cf = new ComapnyMemberCountForm() ;
		cf.setSearchYear(searchYear);
		DataGrid dagagrid = this.comapnyMemberCountService.dagagrid(cf) ;
		List<Map<String, Object>> rows = (List<Map<String, Object>>) dagagrid.getRows() ;
		
		float t1 = 0,t2 = 0,t3 = 0,t4 = 0,t5 = 0,t6 = 0,t7 = 0,t8 = 0,t9 = 0,t10 = 0,t11 = 0,t12 = 0;
		for(int i=0; i< rows.size(); i++) {
			Map<String, Object> m = rows.get(i) ;
			list.add(m);
			
			t1+=m.get("m1")==null?0:Float.parseFloat(m.get("m1").toString());
			t2+=m.get("m2")==null?0:Float.parseFloat(m.get("m2").toString());
			t3+=m.get("m3")==null?0:Float.parseFloat(m.get("m3").toString());
			t4+=m.get("m4")==null?0:Float.parseFloat(m.get("m4").toString());
			t5+=m.get("m5")==null?0:Float.parseFloat(m.get("m5").toString());
			t6+=m.get("m6")==null?0:Float.parseFloat(m.get("m6").toString());
			t7+=m.get("m7")==null?0:Float.parseFloat(m.get("m7").toString());
			t8+=m.get("m8")==null?0:Float.parseFloat(m.get("m8").toString());
			t9+=m.get("m9")==null?0:Float.parseFloat(m.get("m9").toString());
			t10+=m.get("m10")==null?0:Float.parseFloat(m.get("m10").toString());
			t11+=m.get("m11")==null?0:Float.parseFloat(m.get("m11").toString());
			t12+=m.get("m12")==null?0:Float.parseFloat(m.get("m12").toString());
		}
		m11.put("m1", t1);
		m11.put("m2", t2);
		m11.put("m3", t3);
		m11.put("m4", t4);
		m11.put("m5", t5);
		m11.put("m6", t6);
		m11.put("m7", t7);
		m11.put("m8", t8);
		m11.put("m9", t9);
		m11.put("m10", t10);
		m11.put("m11", t11);
		m11.put("m12", t12);
		
		
		String sqlTotal = 
						"select b.category, "+ 
						"sum(b.m1) as tm1, sum(b.m2) as tm2, sum(b.m3) as tm3, sum(b.m4) as tm4, sum(b.m5) as tm5, "+
						"sum(b.m6) as tm6, sum(b.m7) as tm7, sum(b.m8) as tm8, sum(b.m9) as tm9, sum(b.m10) as tm10, "+
						"sum(b.m11) as tm11, sum(b.m12) as tm12 "+
						"FROM ieasy_oa_person_budget b WHERE b.year=? GROUP BY b.category";
		
		
		List<PersonBudgetForm> totalByYearList = this.basedaoPersonBudget.listSQL(sqlTotal, new Object[]{searchYear}, PersonBudgetForm.class, false);
		
		for (PersonBudgetForm p : totalByYearList) {
			if(p.getCategory().equals("deptCount")) {
				m1.put("m1", p.getTm1()==null?0:p.getTm1());
				m1.put("m2", p.getTm2()==null?0:p.getTm2());
				m1.put("m3", p.getTm3()==null?0:p.getTm3());
				m1.put("m4", p.getTm4()==null?0:p.getTm4());
				m1.put("m5", p.getTm5()==null?0:p.getTm5());
				m1.put("m6", p.getTm6()==null?0:p.getTm6());
				m1.put("m7", p.getTm7()==null?0:p.getTm7());
				m1.put("m8", p.getTm8()==null?0:p.getTm8());
				m1.put("m9", p.getTm9()==null?0:p.getTm9());
				m1.put("m10", p.getTm10()==null?0:p.getTm10());
				m1.put("m11", p.getTm11()==null?0:p.getTm11());
				m1.put("m12", p.getTm12()==null?0:p.getTm12());
			}
			if(p.getCategory().equals("maxMember")) {
				m2.put("m1", p.getTm1()==null?0:p.getTm1());
				m2.put("m2", p.getTm2()==null?0:p.getTm2());
				m2.put("m3", p.getTm3()==null?0:p.getTm3());
				m2.put("m4", p.getTm4()==null?0:p.getTm4());
				m2.put("m5", p.getTm5()==null?0:p.getTm5());
				m2.put("m6", p.getTm6()==null?0:p.getTm6());
				m2.put("m7", p.getTm7()==null?0:p.getTm7());
				m2.put("m8", p.getTm8()==null?0:p.getTm8());
				m2.put("m9", p.getTm9()==null?0:p.getTm9());
				m2.put("m10", p.getTm10()==null?0:p.getTm10());
				m2.put("m11", p.getTm11()==null?0:p.getTm11());
				m2.put("m12", p.getTm12()==null?0:p.getTm12());
			}
			if(p.getCategory().equals("minMember")) {
				m3.put("m1", p.getTm1()==null?0:p.getTm1());
				m3.put("m2", p.getTm2()==null?0:p.getTm2());
				m3.put("m3", p.getTm3()==null?0:p.getTm3());
				m3.put("m4", p.getTm4()==null?0:p.getTm4());
				m3.put("m5", p.getTm5()==null?0:p.getTm5());
				m3.put("m6", p.getTm6()==null?0:p.getTm6());
				m3.put("m7", p.getTm7()==null?0:p.getTm7());
				m3.put("m8", p.getTm8()==null?0:p.getTm8());
				m3.put("m9", p.getTm9()==null?0:p.getTm9());
				m3.put("m10", p.getTm10()==null?0:p.getTm10());
				m3.put("m11", p.getTm11()==null?0:p.getTm11());
				m3.put("m12", p.getTm12()==null?0:p.getTm12());
			}
		}
		
		if(null != totalByYearList && totalByYearList.size() > 0) {
			//计算需求平均（人）
			m4.put("m1", NumberUtil.setScale((Float.parseFloat(m2.get("m1").toString())+Float.parseFloat(m3.get("m1").toString()))/2));
			m4.put("m2", NumberUtil.setScale((Float.parseFloat(m2.get("m2").toString())+Float.parseFloat(m3.get("m2").toString()))/2));
			m4.put("m3", NumberUtil.setScale((Float.parseFloat(m2.get("m3").toString())+Float.parseFloat(m3.get("m3").toString()))/2));
			m4.put("m4", NumberUtil.setScale((Float.parseFloat(m2.get("m4").toString())+Float.parseFloat(m3.get("m4").toString()))/2));
			m4.put("m5", NumberUtil.setScale((Float.parseFloat(m2.get("m5").toString())+Float.parseFloat(m3.get("m5").toString()))/2));
			m4.put("m6", NumberUtil.setScale((Float.parseFloat(m2.get("m6").toString())+Float.parseFloat(m3.get("m6").toString()))/2));
			m4.put("m7", NumberUtil.setScale((Float.parseFloat(m2.get("m7").toString())+Float.parseFloat(m3.get("m7").toString()))/2));
			m4.put("m8", NumberUtil.setScale((Float.parseFloat(m2.get("m8").toString())+Float.parseFloat(m3.get("m8").toString()))/2));
			m4.put("m9", NumberUtil.setScale((Float.parseFloat(m2.get("m9").toString())+Float.parseFloat(m3.get("m9").toString()))/2));
			m4.put("m10", NumberUtil.setScale((Float.parseFloat(m2.get("m10").toString())+Float.parseFloat(m3.get("m10").toString()))/2));
			m4.put("m11", NumberUtil.setScale((Float.parseFloat(m2.get("m11").toString())+Float.parseFloat(m3.get("m11").toString()))/2));
			m4.put("m12", NumberUtil.setScale((Float.parseFloat(m2.get("m12").toString())+Float.parseFloat(m3.get("m12").toString()))/2));
			
			//最大空闲人数（人）
			m5.put("m1", NumberUtil.setScale((Float.parseFloat(m11.get("m1").toString())-Float.parseFloat(m3.get("m1").toString()))));
			m5.put("m2", NumberUtil.setScale((Float.parseFloat(m11.get("m2").toString())-Float.parseFloat(m3.get("m2").toString()))));
			m5.put("m3", NumberUtil.setScale((Float.parseFloat(m11.get("m3").toString())-Float.parseFloat(m3.get("m3").toString()))));
			m5.put("m4", NumberUtil.setScale((Float.parseFloat(m11.get("m4").toString())-Float.parseFloat(m3.get("m4").toString()))));
			m5.put("m5", NumberUtil.setScale((Float.parseFloat(m11.get("m5").toString())-Float.parseFloat(m3.get("m5").toString()))));
			m5.put("m6", NumberUtil.setScale((Float.parseFloat(m11.get("m6").toString())-Float.parseFloat(m3.get("m6").toString()))));
			m5.put("m7", NumberUtil.setScale((Float.parseFloat(m11.get("m7").toString())-Float.parseFloat(m3.get("m7").toString()))));
			m5.put("m8", NumberUtil.setScale((Float.parseFloat(m11.get("m8").toString())-Float.parseFloat(m3.get("m8").toString()))));
			m5.put("m9", NumberUtil.setScale((Float.parseFloat(m11.get("m9").toString())-Float.parseFloat(m3.get("m9").toString()))));
			m5.put("m10", NumberUtil.setScale((Float.parseFloat(m11.get("m10").toString())-Float.parseFloat(m3.get("m10").toString()))));
			m5.put("m11", NumberUtil.setScale((Float.parseFloat(m11.get("m11").toString())-Float.parseFloat(m3.get("m11").toString()))));
			m5.put("m12", NumberUtil.setScale((Float.parseFloat(m11.get("m12").toString())-Float.parseFloat(m3.get("m12").toString()))));
			
			//最小空闲人数（人）
			m6.put("m1", NumberUtil.setScale((Float.parseFloat(m11.get("m1").toString())-Float.parseFloat(m2.get("m1").toString()))));
			m6.put("m2", NumberUtil.setScale((Float.parseFloat(m11.get("m2").toString())-Float.parseFloat(m2.get("m2").toString()))));
			m6.put("m3", NumberUtil.setScale((Float.parseFloat(m11.get("m3").toString())-Float.parseFloat(m2.get("m3").toString()))));
			m6.put("m4", NumberUtil.setScale((Float.parseFloat(m11.get("m4").toString())-Float.parseFloat(m2.get("m4").toString()))));
			m6.put("m5", NumberUtil.setScale((Float.parseFloat(m11.get("m5").toString())-Float.parseFloat(m2.get("m5").toString()))));
			m6.put("m6", NumberUtil.setScale((Float.parseFloat(m11.get("m6").toString())-Float.parseFloat(m2.get("m6").toString()))));
			m6.put("m7", NumberUtil.setScale((Float.parseFloat(m11.get("m7").toString())-Float.parseFloat(m2.get("m7").toString()))));
			m6.put("m8", NumberUtil.setScale((Float.parseFloat(m11.get("m8").toString())-Float.parseFloat(m2.get("m8").toString()))));
			m6.put("m9", NumberUtil.setScale((Float.parseFloat(m11.get("m9").toString())-Float.parseFloat(m2.get("m9").toString()))));
			m6.put("m10", NumberUtil.setScale((Float.parseFloat(m11.get("m10").toString())-Float.parseFloat(m2.get("m10").toString()))));
			m6.put("m11", NumberUtil.setScale((Float.parseFloat(m11.get("m11").toString())-Float.parseFloat(m2.get("m11").toString()))));
			m6.put("m12", NumberUtil.setScale((Float.parseFloat(m11.get("m12").toString())-Float.parseFloat(m2.get("m12").toString()))));
			
			//空闲平均（人）
			m7.put("m1", NumberUtil.setScale((Float.parseFloat(m5.get("m1").toString())+Float.parseFloat(m6.get("m1").toString()))/2));
			m7.put("m2", NumberUtil.setScale((Float.parseFloat(m5.get("m2").toString())+Float.parseFloat(m6.get("m2").toString()))/2));
			m7.put("m3", NumberUtil.setScale((Float.parseFloat(m5.get("m3").toString())+Float.parseFloat(m6.get("m3").toString()))/2));
			m7.put("m4", NumberUtil.setScale((Float.parseFloat(m5.get("m4").toString())+Float.parseFloat(m6.get("m4").toString()))/2));
			m7.put("m5", NumberUtil.setScale((Float.parseFloat(m5.get("m5").toString())+Float.parseFloat(m6.get("m5").toString()))/2));
			m7.put("m6", NumberUtil.setScale((Float.parseFloat(m5.get("m6").toString())+Float.parseFloat(m6.get("m6").toString()))/2));
			m7.put("m7", NumberUtil.setScale((Float.parseFloat(m5.get("m7").toString())+Float.parseFloat(m6.get("m7").toString()))/2));
			m7.put("m8", NumberUtil.setScale((Float.parseFloat(m5.get("m8").toString())+Float.parseFloat(m6.get("m8").toString()))/2));
			m7.put("m9", NumberUtil.setScale((Float.parseFloat(m5.get("m9").toString())+Float.parseFloat(m6.get("m9").toString()))/2));
			m7.put("m10", NumberUtil.setScale((Float.parseFloat(m5.get("m10").toString())+Float.parseFloat(m6.get("m10").toString()))/2));
			m7.put("m11", NumberUtil.setScale((Float.parseFloat(m5.get("m11").toString())+Float.parseFloat(m6.get("m11").toString()))/2));
			m7.put("m12", NumberUtil.setScale((Float.parseFloat(m5.get("m12").toString())+Float.parseFloat(m6.get("m12").toString()))/2));
		}
		
		list.add(m11) ;
		
		list.add(m1) ;
		list.add(m2) ;
		list.add(m3) ;
		
		list.add(m4) ;
		list.add(m5) ;
		list.add(m6) ;
		list.add(m7) ;
		
		
		return list ;
	}
	
	@Override
	public DataGrid dagagrid(PersonBudgetForm form) {
		DataGrid dg = new DataGrid() ;
		
		PersonForm p = (PersonForm) this.basedaoPerson.queryObjectSQL("select t.id, t.num, t.name from ieasy_sys_person t where t.num=?", new Object[]{form.getPersonId()}, PersonForm.class, false) ;
		
		if(null == p) {
			return dg; 
		}
		form.setPersonId(p.getId());
		dg.setRows(this.getPersonBudget(form));
		return dg;
	}
	
	private List<Map<String, Object>> getPersonBudget(PersonBudgetForm form) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>() ;
		
		//个人（部长）显示的数据只有6行
		Map<String, Object> m1 = new HashMap<String, Object>() ;
		m1.put("category", "本部門人数（人）") ;
		Map<String, Object> m2 = new HashMap<String, Object>() ;
		m2.put("category", "最大需求人数（人）") ;
		Map<String, Object> m3 = new HashMap<String, Object>() ;
		m3.put("category", "最小需求人数（人）") ;
		
		Map<String, Object> m4 = new HashMap<String, Object>() ;
		m4.put("category", "需求平均（人）") ;
		Map<String, Object> m5 = new HashMap<String, Object>() ;
		m5.put("category", "空闲人数上限（人）") ;
		Map<String, Object> m6 = new HashMap<String, Object>() ;
		m6.put("category", "空闲人数下限（人）") ;
		Map<String, Object> m7 = new HashMap<String, Object>() ;
		m7.put("category", "空闲平均（人）") ;
		
		
		//获取个人的数据
		List<PersonBudgetForm> personBudgetList = this.find(form) ;
		for (PersonBudgetForm p : personBudgetList) {
			if(p.getCategory().equals("deptCount")) {
				m1.put("m1", p.getM1()==null?0:p.getM1());
				m1.put("m2", p.getM2()==null?0:p.getM2());
				m1.put("m3", p.getM3()==null?0:p.getM3());
				m1.put("m4", p.getM4()==null?0:p.getM4());
				m1.put("m5", p.getM5()==null?0:p.getM5());
				m1.put("m6", p.getM6()==null?0:p.getM6());
				m1.put("m7", p.getM7()==null?0:p.getM7());
				m1.put("m8", p.getM8()==null?0:p.getM8());
				m1.put("m9", p.getM9()==null?0:p.getM9());
				m1.put("m10", p.getM10()==null?0:p.getM10());
				m1.put("m11", p.getM11()==null?0:p.getM11());
				m1.put("m12", p.getM12()==null?0:p.getM12());
			}
			if(p.getCategory().equals("maxMember")) {
				m2.put("m1", p.getM1()==null?0:p.getM1());
				m2.put("m2", p.getM2()==null?0:p.getM2());
				m2.put("m3", p.getM3()==null?0:p.getM3());
				m2.put("m4", p.getM4()==null?0:p.getM4());
				m2.put("m5", p.getM5()==null?0:p.getM5());
				m2.put("m6", p.getM6()==null?0:p.getM6());
				m2.put("m7", p.getM7()==null?0:p.getM7());
				m2.put("m8", p.getM8()==null?0:p.getM8());
				m2.put("m9", p.getM9()==null?0:p.getM9());
				m2.put("m10", p.getM10()==null?0:p.getM10());
				m2.put("m11", p.getM11()==null?0:p.getM11());
				m2.put("m12", p.getM12()==null?0:p.getM12());
			}
			if(p.getCategory().equals("minMember")) {
				m3.put("m1", p.getM1()==null?0:p.getM1());
				m3.put("m2", p.getM2()==null?0:p.getM2());
				m3.put("m3", p.getM3()==null?0:p.getM3());
				m3.put("m4", p.getM4()==null?0:p.getM4());
				m3.put("m5", p.getM5()==null?0:p.getM5());
				m3.put("m6", p.getM6()==null?0:p.getM6());
				m3.put("m7", p.getM7()==null?0:p.getM7());
				m3.put("m8", p.getM8()==null?0:p.getM8());
				m3.put("m9", p.getM9()==null?0:p.getM9());
				m3.put("m10", p.getM10()==null?0:p.getM10());
				m3.put("m11", p.getM11()==null?0:p.getM11());
				m3.put("m12", p.getM12()==null?0:p.getM12());
			}
		}
		
		if(null != personBudgetList && personBudgetList.size() > 0) {
			//计算需求平均（人）
			m4.put("m1", NumberUtil.setScale((Float.parseFloat(m2.get("m1").toString())+Float.parseFloat(m3.get("m1").toString()))/2));
			m4.put("m2", NumberUtil.setScale((Float.parseFloat(m2.get("m2").toString())+Float.parseFloat(m3.get("m2").toString()))/2));
			m4.put("m3", NumberUtil.setScale((Float.parseFloat(m2.get("m3").toString())+Float.parseFloat(m3.get("m3").toString()))/2));
			m4.put("m4", NumberUtil.setScale((Float.parseFloat(m2.get("m4").toString())+Float.parseFloat(m3.get("m4").toString()))/2));
			m4.put("m5", NumberUtil.setScale((Float.parseFloat(m2.get("m5").toString())+Float.parseFloat(m3.get("m5").toString()))/2));
			m4.put("m6", NumberUtil.setScale((Float.parseFloat(m2.get("m6").toString())+Float.parseFloat(m3.get("m6").toString()))/2));
			m4.put("m7", NumberUtil.setScale((Float.parseFloat(m2.get("m7").toString())+Float.parseFloat(m3.get("m7").toString()))/2));
			m4.put("m8", NumberUtil.setScale((Float.parseFloat(m2.get("m8").toString())+Float.parseFloat(m3.get("m8").toString()))/2));
			m4.put("m9", NumberUtil.setScale((Float.parseFloat(m2.get("m9").toString())+Float.parseFloat(m3.get("m9").toString()))/2));
			m4.put("m10", NumberUtil.setScale((Float.parseFloat(m2.get("m10").toString())+Float.parseFloat(m3.get("m10").toString()))/2));
			m4.put("m11", NumberUtil.setScale((Float.parseFloat(m2.get("m11").toString())+Float.parseFloat(m3.get("m11").toString()))/2));
			m4.put("m12", NumberUtil.setScale((Float.parseFloat(m2.get("m12").toString())+Float.parseFloat(m3.get("m12").toString()))/2));
			
			//空闲人数上限（人）
			m5.put("m1", NumberUtil.setScale((Float.parseFloat(m1.get("m1").toString())-Float.parseFloat(m3.get("m1").toString()))));
			m5.put("m2", NumberUtil.setScale((Float.parseFloat(m1.get("m2").toString())-Float.parseFloat(m3.get("m2").toString()))));
			m5.put("m3", NumberUtil.setScale((Float.parseFloat(m1.get("m3").toString())-Float.parseFloat(m3.get("m3").toString()))));
			m5.put("m4", NumberUtil.setScale((Float.parseFloat(m1.get("m4").toString())-Float.parseFloat(m3.get("m4").toString()))));
			m5.put("m5", NumberUtil.setScale((Float.parseFloat(m1.get("m5").toString())-Float.parseFloat(m3.get("m5").toString()))));
			m5.put("m6", NumberUtil.setScale((Float.parseFloat(m1.get("m6").toString())-Float.parseFloat(m3.get("m6").toString()))));
			m5.put("m7", NumberUtil.setScale((Float.parseFloat(m1.get("m7").toString())-Float.parseFloat(m3.get("m7").toString()))));
			m5.put("m8", NumberUtil.setScale((Float.parseFloat(m1.get("m8").toString())-Float.parseFloat(m3.get("m8").toString()))));
			m5.put("m9", NumberUtil.setScale((Float.parseFloat(m1.get("m9").toString())-Float.parseFloat(m3.get("m9").toString()))));
			m5.put("m10", NumberUtil.setScale((Float.parseFloat(m1.get("m10").toString())-Float.parseFloat(m3.get("m10").toString()))));
			m5.put("m11", NumberUtil.setScale((Float.parseFloat(m1.get("m11").toString())-Float.parseFloat(m3.get("m11").toString()))));
			m5.put("m12", NumberUtil.setScale((Float.parseFloat(m1.get("m12").toString())-Float.parseFloat(m3.get("m12").toString()))));
			
			//空闲人数下限（人）
			m6.put("m1", NumberUtil.setScale((Float.parseFloat(m1.get("m1").toString())-Float.parseFloat(m2.get("m1").toString()))));
			m6.put("m2", NumberUtil.setScale((Float.parseFloat(m1.get("m2").toString())-Float.parseFloat(m2.get("m2").toString()))));
			m6.put("m3", NumberUtil.setScale((Float.parseFloat(m1.get("m3").toString())-Float.parseFloat(m2.get("m3").toString()))));
			m6.put("m4", NumberUtil.setScale((Float.parseFloat(m1.get("m4").toString())-Float.parseFloat(m2.get("m4").toString()))));
			m6.put("m5", NumberUtil.setScale((Float.parseFloat(m1.get("m5").toString())-Float.parseFloat(m2.get("m5").toString()))));
			m6.put("m6", NumberUtil.setScale((Float.parseFloat(m1.get("m6").toString())-Float.parseFloat(m2.get("m6").toString()))));
			m6.put("m7", NumberUtil.setScale((Float.parseFloat(m1.get("m7").toString())-Float.parseFloat(m2.get("m7").toString()))));
			m6.put("m8", NumberUtil.setScale((Float.parseFloat(m1.get("m8").toString())-Float.parseFloat(m2.get("m8").toString()))));
			m6.put("m9", NumberUtil.setScale((Float.parseFloat(m1.get("m9").toString())-Float.parseFloat(m2.get("m9").toString()))));
			m6.put("m10", NumberUtil.setScale((Float.parseFloat(m1.get("m10").toString())-Float.parseFloat(m2.get("m10").toString()))));
			m6.put("m11", NumberUtil.setScale((Float.parseFloat(m1.get("m11").toString())-Float.parseFloat(m2.get("m11").toString()))));
			m6.put("m12", NumberUtil.setScale((Float.parseFloat(m1.get("m12").toString())-Float.parseFloat(m2.get("m12").toString()))));
			
			//空闲平均（人）
			m7.put("m1", NumberUtil.setScale((Float.parseFloat(m5.get("m1").toString())+Float.parseFloat(m6.get("m1").toString()))/2));
			m7.put("m2", NumberUtil.setScale((Float.parseFloat(m5.get("m2").toString())+Float.parseFloat(m6.get("m2").toString()))/2));
			m7.put("m3", NumberUtil.setScale((Float.parseFloat(m5.get("m3").toString())+Float.parseFloat(m6.get("m3").toString()))/2));
			m7.put("m4", NumberUtil.setScale((Float.parseFloat(m5.get("m4").toString())+Float.parseFloat(m6.get("m4").toString()))/2));
			m7.put("m5", NumberUtil.setScale((Float.parseFloat(m5.get("m5").toString())+Float.parseFloat(m6.get("m5").toString()))/2));
			m7.put("m6", NumberUtil.setScale((Float.parseFloat(m5.get("m6").toString())+Float.parseFloat(m6.get("m6").toString()))/2));
			m7.put("m7", NumberUtil.setScale((Float.parseFloat(m5.get("m7").toString())+Float.parseFloat(m6.get("m7").toString()))/2));
			m7.put("m8", NumberUtil.setScale((Float.parseFloat(m5.get("m8").toString())+Float.parseFloat(m6.get("m8").toString()))/2));
			m7.put("m9", NumberUtil.setScale((Float.parseFloat(m5.get("m9").toString())+Float.parseFloat(m6.get("m9").toString()))/2));
			m7.put("m10", NumberUtil.setScale((Float.parseFloat(m5.get("m10").toString())+Float.parseFloat(m6.get("m10").toString()))/2));
			m7.put("m11", NumberUtil.setScale((Float.parseFloat(m5.get("m11").toString())+Float.parseFloat(m6.get("m11").toString()))/2));
			m7.put("m12", NumberUtil.setScale((Float.parseFloat(m5.get("m12").toString())+Float.parseFloat(m6.get("m12").toString()))/2));
		}
		
		list.add(m1) ;
		list.add(m2) ;
		list.add(m3) ;
		
		list.add(m4) ;
		list.add(m5) ;
		list.add(m6) ;
		list.add(m7) ;
		
		
		return list ;
	}
	
	private List<PersonBudgetForm> find(PersonBudgetForm form) {
		Map<String, Object> alias = new HashMap<String, Object>();
		String sql = "select " +
					 "t.id, t.personId, t.year, t.category, "+
					 "t.m1, t.m2, t.m3, t.m4, t.m5, t.m6, t.m7, t.m8, t.m9, t.m10, t.m11, t.m12, " +
					 "p.name as personName " +
					 "from ieasy_oa_person_budget t " +
				 	 "left join ieasy_sys_person p on(p.id=t.personId) " +
				 	 "where 1=1 ";
		
		
		sql = addWhere(sql, form, alias) ;
		return this.basedaoPersonBudget.listSQL(sql, alias, PersonBudgetForm.class, false) ;
	}
	
	private String addWhere(String sql, PersonBudgetForm form, Map<String, Object> params) {
		if (null != form) {
			if (form.getPersonId() != null && !"".equals(form.getPersonId().trim())) {
				sql += " and t.personId =:personId";
				params.put("personId", form.getPersonId().trim());
			}
			sql += " and t.year =:year";
			params.put("year", form.getSearchYear());
		}
		return sql ;
	}

	@Override
	public void deleteByPerson(String id) {
		this.basedaoPersonBudget.executeSQL("delete from ieasy_oa_person_budget where personId=?", new Object[]{id}) ;
	}

}
